﻿using Set_File_Rw;
using System;
using System.Data.SqlClient;
using System.IO;
using System.Windows;

namespace TestFireBroadcastingHostPhone
{
    public partial class Loading : Window
    {
        /// <summary>
        /// 配置文件保存路径
        /// </summary>
        private const string configpath = "C:\\广播电话生产测试软件配置\\SecondDetector\\RelatedConfig.ini";
        public Loading()
        {
            InitializeComponent();
            string err_info = string.Empty;
            string default_file_path = Directory.GetCurrentDirectory() + "\\SetConfig\\RelatedConfig.ini";
            SQL_Name.Text = Set_Data_Rw.Read(configpath, default_file_path, "服务器名称", out err_info);
            User_Name.Text = Set_Data_Rw.Read(configpath, default_file_path, "用户名", out err_info);
            SQL_Code.Password = Set_Data_Rw.Read(configpath, default_file_path, "数据库密码", out err_info);
        }
        public string serverName = string.Empty;
        public string userName = string.Empty;
        public string userPassword = string.Empty;

        /// <summary>
        /// 用户登录程序
        /// </summary>
        /// <returns></returns>
        public bool UserLoad()
        {
            string err_info = string.Empty;
            bool loginSuccessful = false;
            serverName = SQL_Name.Text;
            userName = User_Name.Text;
            userPassword = SQL_Code.Password;
            string connectionString = "DATA SOURCE=" + serverName + ";INITIAL CATALOG=" + "盈帜探测器测试" + ";USER ID=" + userName + ";PWD=" + userPassword;
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                try
                {
                    connection.Open();
                    loginSuccessful = true;
                }
                catch (Exception ex)
                {
                    // 记录错误并处理异常
                    err_info = ex.Message;
                    loginSuccessful = false;
                }
            }
            return loginSuccessful;
        }
        /// <summary>
        /// 用户登录数据库界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_UserLoadingClick(object sender, RoutedEventArgs e)
        {
            string err_info = string.Empty;
            string default_file_path = Directory.GetCurrentDirectory() + "\\SetConfig\\RelatedConfig.ini";
            if (UserLoad())
            {
                Set_Data_Rw.Write(configpath, default_file_path, "服务器名称", SQL_Name.Text, out err_info);
                Set_Data_Rw.Write(configpath, default_file_path, "用户名", User_Name.Text, out err_info);
                Set_Data_Rw.Write(configpath, default_file_path, "数据库密码", SQL_Code.Password, out err_info);
                // 创建并显示主窗口实例
                var mainWindow = new SystemConfig();
                mainWindow.Show();
                this.Close();
            }
            else
            {
                MessageBox.Show("登录失败，请检查用户名和数据库密码");
            }
        }

        /// <summary>
        /// 退出软件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_QuitAppClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
